#include "VSUserConstant.h"
#include "VSResourceManager.h"
#include "VSGraphicInclude.h"
#include "VSStream.h"
using namespace VSEngine2;
IMPLEMENT_RTTI(VSUserConstant,VSObject)
BEGIN_ADD_PROPERTY(VSUserConstant,VSObject)
REGISTER_PROPERTY(m_uiValueType,ValueType,VSProperty::F_SAVE_LOAD_CLONE)
REGISTER_PROPERTY(m_uiRegisterIndex,RegisterIndex,VSProperty::F_SAVE_LOAD_CLONE)
REGISTER_PROPERTY(m_uiRegisterNum,RegisterNum,VSProperty::F_SAVE_LOAD_CLONE)
REGISTER_PROPERTY(m_Name,Name,VSProperty::F_SAVE_LOAD_CLONE)
REGISTER_PROPERTY_DATA(m_pDate,m_uiSize,Date);
END_ADD_PROPERTY
IMPLEMENT_INITIAL_BEGIN(VSUserConstant)
IMPLEMENT_INITIAL_END
VSUserConstant::VSUserConstant()
{
	m_pDate = NULL;
	m_uiRegisterIndex = 0;
	m_uiValueType = VSMAX_INTEGER;
	m_uiRegisterNum = 0;
	m_uiSize = 0;
}
VSUserConstant::VSUserConstant(const VSUsedName & Name,const void * pDate,
							   unsigned int uiSize,
							  unsigned int uiRegisterIndex,unsigned int uiRegisterNum,
							   unsigned int uiValueType)
{
	VSMAC_ASSERT(uiValueType < VT_MAX && uiSize > 0 && uiRegisterNum > 0);

	m_Name = Name;
	m_uiValueType = uiValueType;
	m_uiSize = uiSize;
	m_pDate = new unsigned char[uiSize];
	m_uiRegisterIndex = uiRegisterIndex;	
	m_uiRegisterNum = uiRegisterNum;
	VSMAC_ASSERT(m_pDate);

	if(pDate)
	{
		VSMemcpy(m_pDate,pDate,uiSize);
	}
	else
	{
		VSMemset(m_pDate,0,uiSize);
	}

}
VSUserConstant::~VSUserConstant()
{
	VSMAC_DELETEA(m_pDate);

}


bool VSUserConstant::SetDate(void *pElementDate)
{
	if(!pElementDate)
		return 0;
	void * pDest =(unsigned char * )m_pDate;
	VSMemcpy(pDest,pElementDate,m_uiSize);

	return 1;
}
unsigned int VSUserConstant::DiskUse()const
{
	unsigned int Used = VSObject::DiskUse();
	Used += sizeof(unsigned int) * 4;
	Used += m_Name.DiskUse();
	return Used;
}

bool VSUserConstant::Save(VSStream & rStream,unsigned int &iSaveUse)const
{
	if(!VSObject::Save(rStream,iSaveUse))
		return 0;



	if(!m_uiSize)
		return 0;
	if(!rStream.Write(&m_uiSize,sizeof(unsigned int)))
		return 0;
	iSaveUse += sizeof(unsigned int);


	if(!rStream.Write(&m_uiRegisterIndex,sizeof(unsigned int)))
		return 0;
	iSaveUse += sizeof(unsigned int);

	if(!rStream.Write(&m_uiValueType,sizeof(unsigned int)))
		return 0;
	iSaveUse += sizeof(unsigned int);

	if(!rStream.Write(&m_uiRegisterNum,sizeof(unsigned int)))
		return 0;
	iSaveUse += sizeof(unsigned int);

	if (!m_Name.Save(rStream,iSaveUse))
	{
		return 0;
	}
	return 1;
}
bool VSUserConstant::Load(VSStream & rStream,unsigned int &iSaveUse)
{
	if(!VSObject::Load(rStream,iSaveUse))
		return 0;

	if(!rStream.Read(&m_uiSize,sizeof(unsigned int)))
		return 0;
	iSaveUse += sizeof(unsigned int);

	if(!rStream.Read(&m_uiRegisterIndex,sizeof(unsigned int)))
		return 0;
	iSaveUse += sizeof(unsigned int);

	if(!rStream.Read(&m_uiValueType,sizeof(unsigned int)))
		return 0;
	iSaveUse += sizeof(unsigned int);

	if(!rStream.Read(&m_uiRegisterNum,sizeof(unsigned int)))
		return 0;
	iSaveUse += sizeof(unsigned int);

	

	if (!m_Name.Load(rStream,iSaveUse))
	{
		return 0;
	}
	return 1;
}
bool VSUserConstant::PostLoad(void * pDate)
{
	VSMAC_DELETEA(m_pDate);

	m_pDate = VS_NEW unsigned char[m_uiSize];
	if(!m_pDate)
		return 0;
	VSMemset(m_pDate,0,m_uiSize);
	return true;
}
bool VSUserConstant::Clone(const VSObject *pObject,unsigned int uiType)
{
	const VSUserConstant* Temp = DynamicCast<VSUserConstant>(pObject); 
	if(!Temp)
		return 0;
	if(!Temp->m_pDate)
		return 0;

	if(VSObject::Clone(pObject,uiType))
	{
		m_Name			= Temp->m_Name;
		m_uiSize		= Temp->m_uiSize;
		m_uiRegisterIndex	= Temp->m_uiRegisterIndex;
		m_uiValueType	= Temp->m_uiValueType;
		m_uiRegisterNum = Temp->m_uiRegisterNum;
		VSMAC_DELETEA(m_pDate);

		m_pDate = VS_NEW unsigned char[m_uiSize];

		if(!m_pDate)
			return 0;
		VSMemcpy(m_pDate,Temp->m_pDate,m_uiSize);
		return 1;	
	}
	else
		return 0;
}